package java44th.group.service.admin.brand;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java44th.group.bean.Bo.brand.CreateBo;
import java44th.group.bean.Bo.brand.ListBo;
import java44th.group.bean.common.BaseStringUtils;
import java44th.group.bean.po.MarketBrand;
import java44th.group.bean.po.MarketBrandExample;
import java44th.group.bean.admin.vo.brand.CreateVo;
import java44th.group.bean.admin.vo.brand.ListVo;
import java44th.group.mapper.MarketBrandMapper;
import java44th.group.utils.LocalHost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Aurhor: 437
 * @DATE: 2022/0/2 0:4
 * @Description:
 * @Version .0
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    MarketBrandMapper brandMapper;

    //查询品牌商列表
    @Override
    public ListVo brandList(ListBo listBo) {
        //分页查询
        PageHelper.startPage(listBo.getPage(), listBo.getLimit());
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();

        if (!BaseStringUtils.isEmpty(listBo.getId())) {
            criteria.andIdEqualTo(Integer.parseInt(listBo.getId()));
        }

        if (!BaseStringUtils.isEmpty(listBo.getName())) {
            criteria.andNameLike("%"+listBo.getName()+"%");
        }
        List<MarketBrand> marketBrands = brandMapper.selectByExample(marketBrandExample);


        for (MarketBrand marketBrand : marketBrands) {
            if(!marketBrand.getPicUrl().startsWith("http")){
                marketBrand.setPicUrl(LocalHost.localhost + marketBrand.getPicUrl());}
        }


        PageInfo<MarketBrand> marketBrandPageInfo = new PageInfo<>(marketBrands);
        int total = (int) marketBrandPageInfo.getTotal();
        //页数
        int pages = (int) Math.ceil((double) total / (double) listBo.getLimit());
        //给ListVo属性赋值
        ListVo listVo = new ListVo(total, listBo.getPage(), listBo.getLimit(), pages, marketBrands);
        return listVo;


    }

    //新建品牌商
    @Override

    public CreateVo brandCreate(CreateBo createBo) {

        MarketBrand marketBrand = new MarketBrand();
        marketBrand.setDesc(createBo.getDesc());
        marketBrand.setName(createBo.getName());
        BigDecimal floorPrice = BigDecimal.valueOf(Double.parseDouble(createBo.getFloorPrice()));

        marketBrand.setFloorPrice(floorPrice);
        String url = createBo.getPicUrl().replace(LocalHost.localhost, "");

        marketBrand.setPicUrl(url);
        marketBrand.setAddTime(new Date());
        marketBrand.setUpdateTime(new Date());
        // 插入数据,获得自增id

        brandMapper.insertSelective(marketBrand);
        String newUrl = LocalHost.localhost + url;
        CreateVo createVo = new CreateVo(marketBrand.getId(), marketBrand.getName(), marketBrand.getDesc(), newUrl,
                marketBrand.getFloorPrice().intValue(), marketBrand.getAddTime(), marketBrand.getUpdateTime()
        );
        return createVo;

    }
    //修改品牌商
    @Override
    public MarketBrand brandUpdate(MarketBrand marketBrand) {

        marketBrand.setUpdateTime(new Date());

        String replace = marketBrand.getPicUrl().replace(LocalHost.localhost, "");
        marketBrand.setPicUrl(replace);
        brandMapper.updateByPrimaryKeySelective(marketBrand);
        marketBrand.setPicUrl(LocalHost.localhost + marketBrand.getPicUrl());

        return marketBrand;

    }

    //删除品牌商
    @Override
    public void brandDelete(MarketBrand marketBrand) {

        brandMapper.deleteByPrimaryKey(marketBrand.getId());

    }
}
